package com.sven.player;

import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.text.TextUtils;

import java.io.IOException;

/**
 * Created by Sven on 2016/11/29.
 */

public class CustomMediaPlayer {

    private MediaPlayer mp;
    private static CustomMediaPlayer customMediaPlayer;
    String currentPath;
    WatchOnCompletionListener watchOnCompletionListener;
    private Boolean isPlaying = false;
    private CountDownTimer countDownTimer;

    private CustomMediaPlayer() {

        mp = new MediaPlayer();

        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp1) {
                mp.reset();
                watchOnCompletionListener.onCompletion();
            }
        });
    }

    public static CustomMediaPlayer getMediaPlayer() {
        if (customMediaPlayer == null) {
            synchronized (CustomMediaPlayer.class) {
                if (customMediaPlayer == null) {
                    customMediaPlayer = new CustomMediaPlayer();
                }
            }
        }

        return customMediaPlayer;
    }

    public int getCurrentPoint() {
        return mp.getCurrentPosition();
    }

    public int getDuring() {
        return mp.getDuration();
    }

    public String getCurrentPath() {
        return currentPath;
    }

    public void setCurrentPath(String currentPath) {
        this.currentPath = currentPath;
    }

    public void play(String path) {
        if (TextUtils.isEmpty(path)) {
            return;
        }
        watchOnCompletionListener.onStart();
        isPlaying = true;

        try {
            mp.setDataSource(path);
            mp.prepare();
            mp.start();

            countDownTimer = new CountDownTimer(mp.getDuration(), 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    watchOnCompletionListener.onPlaying(new PlayingInfo(millisUntilFinished));
                }

                @Override
                public void onFinish() {

                }
            };
            countDownTimer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stop() {
        watchOnCompletionListener.onCompletion();
        isPlaying = false;
        mp.stop();
        mp.reset();
        countDownTimer.onFinish();
        countDownTimer.cancel();
        watchOnCompletionListener = null;
        currentPath = null;

    }

    public void destory() {
        mp.release();

    }

    public Boolean getPlaying() {
        return isPlaying;
    }

    public void setPlaying(Boolean playing) {
        isPlaying = playing;
    }

    public WatchOnCompletionListener getWatchOnCompletionListener() {
        return watchOnCompletionListener;
    }

    public void setWatchOnCompletionListener(WatchOnCompletionListener watchOnCompletionListener) {
        this.watchOnCompletionListener = watchOnCompletionListener;
    }

    interface WatchOnCompletionListener {
        void onCompletion();

        void onPlaying(PlayingInfo info);

        void onStart();
    }

}
